package edu.arizona.mindseye.swing;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import edu.arizona.mindseye.VideoModel;
import edu.arizona.mindseye.event.Event;
import edu.arizona.mindseye.event.EventListener;
import edu.arizona.mindseye.event.EventManager;
import edu.arizona.mindseye.event.enums.EventType;
import edu.arizona.mindseye.event.enums.Parameter;

public class StatusPanel extends JPanel {

	private JLabel _name;
	private JLabel _frames;
	private JLabel _currentFrame;
	
	public StatusPanel() {
		super();
		
		setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		
		addComponents();
		addListeners();
	}
	
	private void addComponents() { 
		setLayout(new GridBagLayout());
		
		_name = new JLabel("");
		_frames = new JLabel("");
		_currentFrame = new JLabel("");
		
		add(new JLabel("File Name: "), GBC.makeGBC(0, 0, GridBagConstraints.BOTH, 0, 0));
		add(_name, GBC.makeGBC(1, 0, GridBagConstraints.BOTH, 0, 0));
		add(new JPanel(), GBC.makeGBC(2, 0, GridBagConstraints.BOTH, 1, 0));
		
		add(new JLabel("Current Frame: "), GBC.makeGBC(3, 0, GridBagConstraints.BOTH, 0, 0));
		add(_currentFrame, GBC.makeGBC(4, 0, GridBagConstraints.BOTH, 0, 0));
		add(new JPanel(), GBC.makeGBC(5, 0, GridBagConstraints.BOTH, 1, 0));
		
		add(new JLabel("Frame Count: "), GBC.makeGBC(6, 0, GridBagConstraints.BOTH, 0, 0));
		add(_frames, GBC.makeGBC(7, 0, GridBagConstraints.BOTH, 0, 0));
	}
	
	private void addListeners() { 
		EventManager.inst().register(EventType.MOVIE_LOADED, new EventListener() {
			@Override
			public void onEvent(Event e) {
				_name.setText((String) e.getValue(Parameter.name));
				_frames.setText(((Integer) e.getValue(Parameter.frames)) + "");
				_currentFrame.setText("0");
			}
		});
		
		EventManager.inst().register(EventType.MODEL_CHANGED, new EventListener() {
			@Override
			public void onEvent(Event e) {
				VideoModel model = (VideoModel) e.getValue(Parameter.model);
				_currentFrame.setText(model.getIndex() + "");
			} 
		});
	}
}
